#include "Monster.h"
#include "EntityCategory.h"


const float startHealth = 100.f;
const float height = 100.f;

Monster::Monster(b2World* wo, WindowManager* win, float x)
{
	movingRight = true;

	world = wo;
	window = win;

	additional = 0;

	float y = 300;

	width = window->getWidth()-100;

	health = 100;
	
	
	b2BodyDef bodyDef;
	bodyDef.position = b2Vec2((x)/window->getScale(), (y)/window->getScale());
	bodyDef.type = b2_dynamicBody;
    body = world->CreateBody(&bodyDef);
	body->SetGravityScale(0.0f);

	b2PolygonShape shape;
	shape.SetAsBox((width/2)/window->getScale(), (height/2)/window->getScale()); // Creates a box shape. Divide your desired width and height by 2.
    b2FixtureDef fixtureDef;
    fixtureDef.density = 2000.f;
    fixtureDef.friction = 0.4f;
    fixtureDef.shape = &shape;
	fixtureDef.filter.categoryBits = MONSTER;
	fixtureDef.filter.maskBits = CHARACTER | BALL;
	body->SetUserData(this);
    body->CreateFixture(&fixtureDef);

	body->SetFixedRotation(true);


}


Monster::~Monster(void)
{
	world->DestroyBody(body);
}


bool Monster::drawMonster()
{

	if (health > 0 )
	{
		return false;
	}
	else {
		return true;
	}
}

void Monster::hit(float amount, bool trans)
{
	health -= (amount+additional);

	sf::Color target = sf::Color::White;

	float scale = health/startHealth;

	target = sf::Color(target.r * scale, target.g * scale, target.b * scale);

	
	if (trans)
	{
		window->getBackground()->transition(0.2, target);
	}
	additional = 0;
	//height -= startHeight/10;
	//resize = true;

}

void Monster::hit(float amount, sf::Vector2f loc)
{
	if (loc.x >= (window->getScale() * body->GetPosition().x)-getRectangle().x)
	{
		if (loc.x <= (window->getScale() * body->GetPosition().x)+getRectangle().x)
		{
			hit(amount, true);
		}
	}
}


sf::Vector2f Monster::getPosition()
{
	return sf::Vector2f((window->getScale() * body->GetPosition().x), (window->getScale() * body->GetPosition().y));
}

float Monster::getHealthDecimal()
{
	return health/startHealth;
}

sf::Vector2f Monster::getRectangle()
{
	return sf::Vector2f((width/2)*getHealthDecimal(), height/2);
}



void Monster::move()
{
	float velocity = 4.f;

	if ((window->getScale() * body->GetPosition().x)-getRectangle().x <= 50)
	{
		body->SetLinearVelocity(b2Vec2(velocity, 0));
		movingRight = true;
	}
	else if ((window->getScale()*body->GetPosition().x)+getRectangle().x >= (window->getWidth()-50))
	{
		body->SetLinearVelocity(b2Vec2(-velocity, 0));
		movingRight = false;
	}
	else if (movingRight)
	{
		body->SetLinearVelocity(b2Vec2(velocity, 0));
	}
	else
	{
		body->SetLinearVelocity(b2Vec2(-velocity, 0));
	}
}

void Monster::addAdditional(float amount)
{
	additional = amount;
}

void Monster::reset()
{
	health = startHealth;
}